/**
 * Created by taoyuandi on 8/5/14.
 */
Ext.define('JYnovel.controller.system.UserManagerController', {
    extend: 'Ext.app.Controller',
    views: [
        'system.UserManagerView'
    ],
    stores:[
        'system.UserStore',
        'system.RoleStore',
        // 'system.UserStatusStore'
    ],
    models:[
        'system.UserModel',
        'system.RoleModel'
    ],
    init: function() {
        this.control({
            '#userGridPanel':{
                itemcontextmenu:this.itemContextMenu
            },
            '#userGridPanel button[name=searchBtn]':{
                click:this.searchBtnClick,
            },
            '#userGridPanel button[name=userAddBtn]':{
                click:this.userAddBtnClick
            },
            'system_UserInfoForm button[name=formSaveBtn]':{
                click:this.formSaveBtnClick
            },
            '#userLockMenu':{
                click:this.userLockMenuClick
            },
            '#userUnLockMenu':{
                click:this.userReuseMenuClick
            },
            '#userEditMenu':{
                click:this.userEditMenuClick
            },
            '#userRestPwdMenu':{
                click:this.userRestPwdMenuClick
            }
        });
    },
    searchBtnClick:function(btn){
        var userGrid=Ext.getCmp('userGridPanel');
        var userStore=userGrid.getStore();
        var userName=userGrid.queryById('userName').getValue();
        var userRole=userGrid.queryById('userRole').getValue();
        var status=userGrid.queryById('status').getValue();
        userStore.getProxy().setExtraParam('userName',userName);
        userStore.getProxy().setExtraParam('roleId',userRole);
        userStore.getProxy().setExtraParam('status',status);
        userStore.load();
    },
    userReuseMenuClick:function(menu){
        var userView=Ext.getCmp('userGridPanel');
        var row=userView.getSelectionModel().getSelection()[0];
        Ext.Msg.confirm('提示','确定要开启用户('+row.raw.loginName+')吗？',function(btn) {
            if(btn=='yes'){
                ExtAjaxSubmit({
                    url:'/system/user/status/change',
                    method:'POST',
                    alertError:true,
                    params:{loginName: row.raw.loginName, isLock:0}
                },function (result) {
                    if(result.code==0){
                        Ext.Msg.alert('提示', '操作成功');
                        userView.getStore().reload();
                        userView.getSelectionModel().clearSelections();
                    }
                });
            }
        });
    },
    userLockMenuClick:function(menu){
        var userView=Ext.getCmp('userGridPanel');
        var row=userView.getSelectionModel().getSelection()[0];
        Ext.Msg.confirm('提示','确定要锁定用户('+row.raw.loginName+')吗？',function(btn){
            if(btn=='yes'){
                ExtAjaxSubmit({
                        url:'/system/user/status/change',
                        method:'POST',
                        alertError:true,
                        params:{loginName:row.raw.loginName,isLock:1},

                    },function(result){
                        if(result.code==0){
                            Ext.Msg.alert('提示','锁定成功');
                            userView.getStore().reload();
                            userView.getSelectionModel().clearSelections();
                        }
                 });
            }
        });
    },
    userAddBtnClick:function(){
        var form=Ext.create('JYnovel.view.system.UserInfoForm');
        var formData=new Object();
        formData.url='/system/user/add';
        formData.type='add';
        form.formData=formData;
        Ext.create('Ext.window.Window', {
            title: '账户添加',
            y:100,
            height: 310,
            width: 400,
            layout: 'fit',
            modal:true,
            items:form
        }).show();
    },
    formSaveBtnClick:function(btn){
        var form=btn.findParentByType('form');
        var win=form.findParentByType('window');
        var formData=form.formData;
        ExtSubmitForm(form,formData.url,function(action){
                if (action){
                    win.close();
                    var grid=Ext.getCmp('userGridPanel');
                    grid.getStore().load();
                    Ext.Msg.alert('提示','操作成功');
                }
        });
    },
    userEditMenuClick:function(){
        var grid=Ext.getCmp('userGridPanel');
        var row=grid.getSelectionModel().getSelection()[0];
        var form=Ext.create('JYnovel.view.system.UserInfoForm',{
            loader:ExtCreateLoader({url:'/system/user/details',
                params:{loginName:row.raw.loginName},autoLoad:true,ajaxOptions:{method:'GET'}},
                function(data){
                form.getForm().findField('pwd1').hide().setDisabled(true);
                form.getForm().findField('pwd2').hide().setDisabled(true);
                form.getForm().findField('loginName').setValue(data.loginName).setReadOnly(true);
                form.getForm().findField('nickName').setValue(data.nickName);
                // var roleId=form.getForm().findField('roleId');
                // roleId.getStore().load({
                //     callback:function(records,operation,success){
                //         roleId.setValue(data.roleId);
                //     }
                // });
            },true)
        });
        var formData=new Object();
        formData.url='/system/user/edit';
        formData.type='edit';
        formData.raw=row.raw;
        form.formData=formData;
        Ext.create('Ext.window.Window', {
            title: '账户编辑',
            height: 250,
            width: 400,
            layout: 'fit',
            modal:true,
            items:form
        }).show();
    },
    userRestPwdMenuClick:function(){
        var grid=Ext.getCmp('userGridPanel');
        var row=grid.getSelectionModel().getSelection()[0];
        var form=Ext.create('JYnovel.view.system.UserInfoForm',{
            loader:ExtCreateLoader({url:'/system/user/details',
                params:{loginName:row.raw.loginName},autoLoad:true,ajaxOptions:{method:'GET'}},function(data){
                form.getForm().findField('loginName').setValue(data.loginName).setReadOnly(true);
                form.getForm().findField('nickName').hide().setDisabled(true);
                form.getForm().findField('roleId').hide().setDisabled(true);
            },true)
        });
        var formData=new Object();
        formData.url='/system/user/resetpwd';
        formData.type='resetpwd';
        formData.raw=row.raw;
        form.formData=formData;
        Ext.create('Ext.window.Window', {
            title: '重置密码',
            height: 230,
            width: 400,
            layout: 'fit',
            modal:true,
            items:form
        }).show();
    },
    itemContextMenu:function (menu, record, item, index, e, eOpts ) {
        e.preventDefault();
        var data = record.raw;
        var isLock = data.isLock;
        var menu = Ext.getCmp('userGridViewRightMenu');
        if (menu){
            menu.showAt(e.getXY());
        }else {
            menu = Ext.create('JYnovel.view.system.UserGridViewRightMenu').show();
            menu.showAt(e.getXY());
        }
    }
});

